﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Web.Action;
using PowerCreator.LMS.Action.Entity;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_RoleManager_SetBaseAction :PowerCreator.LMS.Web.Base.BasePage
{
    private string _SysColumnID = "1";        //该界面默认加载的栏目类型为1

    PowerCreator.LMS.Web.Action.Action opAction = new PowerCreator.LMS.Web.Action.Action();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["RoleID"] != null)
            {
                string roleid = Request.QueryString["RoleID"].ToString();
                ViewState["RoleID"] = roleid;
            }

            BindRpt();

            InitPage();
        }
    }

    protected override void InitPage()
    {
        chkAll.Text = GetLanageValueByKey("SetBaseAction_rdbtn_YesAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkNoAll.Text = GetLanageValueByKey("SetBaseAction_rdbtn_NoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkYes.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSelect.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_ShowUser.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o5", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_RoleUser.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o4", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }
    /// <summary>
    /// 绑定角色权限信息
    /// </summary>
    private void BindRpt()
    {
        PowerCreator.LMS.Web.Action.Action opAction = new PowerCreator.LMS.Web.Action.Action();
        //DataTable dt = opAction.QueryActionColumn(int.Parse(_SysColumnID));
        string searchc1 = string.Format("ActioncoluType={0} and ActioncoluMenuIsVisible = 1", _SysColumnID);
        DataTable dt = opAction.QueryColumnAction(searchc1, "", opAction.QueryColumnActionCount(searchc1), 1);
        dt.Columns.Add("ActioncoluVariableLG");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["ActioncoluVariableLG"] = GetLanageValueByKey(dt.Rows[i]["ActioncoluVariable"].ToString(), PowerCreator.LMS.Language.LanagetItemType.MenuItemColumn);
        }
        rptColumnType.DataSource = dt;
        rptColumnType.DataBind();

        string search = string.Format("FKRoleactionRoleID={0}", ViewState["RoleID"]);
        int pageSize = opAction.QueryRoleActionCount(search);
        DataTable dtRoleAction = opAction.QueryRoleAction(search, pageSize, 1);
        Hashtable hs = new Hashtable();
        ArrayList list = new ArrayList();
        for (int i = 0; i < dtRoleAction.Rows.Count; i++)
        {
            hs.Add(dtRoleAction.Rows[i]["FKRoleactionActionID"].ToString(), "1");//已经选定的
        }
        
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            Label lblColumnID = (Label)rptColumnType.Items[i].FindControl("lblColumnID");
            DataTable dt1 = opAction.QueryActionByColumnID(int.Parse(lblColumnID.Text));

            //if (IsLanageAction)
            //{
                for (int k = 0; k < dt1.Rows.Count; k++)
                {
                    string an = GetLanageValueByKey(dt1.Rows[k]["ActionVariable"].ToString(), PowerCreator.LMS.Language.LanagetItemType.MenuItem);
                    if (!string.IsNullOrEmpty(an))
                        dt1.Rows[k]["ActionName"] = an;
                    if (dt1.Rows[k]["IsShow"].ToString() == "0")
                    {
                        dt1.Rows[k].Delete();
                    }
                }
            //}
            //chkList.RepeatColumns = dt1.Rows.Count;
            chkList.DataSource = dt1;
            chkList.DataTextField = "ActionName";
            chkList.DataValueField = "ActionID";
            chkList.DataBind();
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                if (hs.ContainsKey(chkList.Items[j].Value))
                    chkList.Items[j].Selected = true;
            }
        }
    }

    //全选
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                chkList.Items[j].Selected = true;
            }
        }
    }
    //取消
    protected void chkNoAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                chkList.Items[j].Selected = false;
            }
        }
    }
    //反选
    protected void chkYes_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                if (chkList.Items[j].Selected)
                    chkList.Items[j].Selected = false;
                else
                    chkList.Items[j].Selected = true;
            }
        }
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                string actionid = chkList.Items[j].Value;
                string search = string.Format("FKRoleactionRoleId={0} and FKRoleactionActionID={1}", ViewState["RoleID"], actionid);
                DataTable dt = opAction.QueryRoleAction(search, 1, 1);

                if (chkList.Items[j].Selected)
                {
                    if (dt.Rows.Count == 0)
                    {
                        VOTRoleAction vo = new VOTRoleAction();
                        vo.FKRoleactionActionID = int.Parse(actionid);
                        vo.FKRoleactionCreatorId = GetLoginEntity.UserID;
                        vo.FKRoleactionRoleId = int.Parse(ViewState["RoleID"].ToString());
                        vo.RoleactionCreateTime = DateTime.Now;
                        opAction.AddRoleAction(vo);
                    }
                }
                else
                {
                    if (dt.Rows.Count == 1)
                        opAction.DelRoleAction(int.Parse(dt.Rows[0]["Roleactionid"].ToString()));
                }
            }
            lbl_Save_Msg.Text = GetLanageValueByKey("SetBaseAction_msg_save1", PowerCreator.LMS.Language.LanagetItemType.Page);
        }
        
    }
    protected void btn_ShowUser_Click(object sender, EventArgs e)
    {
        //用户查看
        string url = string.Format("RoleUser.aspx?RoleID={0}", ViewState["RoleID"]);
        Script.RedirectURL(url);
    }
    protected void btn_RoleUser_Click(object sender, EventArgs e)
    {
        //用户授权
        string url = string.Format("RoleSelectUser.aspx?RoleID={0}", ViewState["RoleID"]);
        Script.RedirectURL(url);
    }
}
